package com.bupt.smart.ui.activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.bupt.smart.ui.fragment.HomeFragment;
import com.bupt.smart.ui.fragment.MarketFragment;
import com.bupt.smart.ui.fragment.MineFragment;
import com.bupt.smart.ui.fragment.NoticeFragment;
import com.bupt.smart.ui.fragment.ServiceFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;

import java.util.ArrayList;

import com.bupt.smart.smart.R;

public class TabActivity extends AppCompatActivity {
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private CommonTabLayout mTabLayout;
    private String[] mTitles = {"首页", "服务", "商城", "通知", "我的"};
    private int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselected, R.mipmap.tab_service_unselected,
            R.mipmap.tab_market_unselected, R.mipmap.tab_notice_unselected,
            R.mipmap.tab_mine_unselected};
    private int[] mIconSelectIds = {
            R.mipmap.tab_home_selected, R.mipmap.tab_service_selected,
            R.mipmap.tab_market_selected, R.mipmap.tab_notice_selected,
            R.mipmap.tab_mine_selected};
    //tab的标题、选中图标、未选中图标
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initData();
        initView();
        int fragmentIndex;
        Intent intent = getIntent();
        if(intent != null)
            if((fragmentIndex = intent.getIntExtra("fragment",-1))!=-1) // 直接跳转商城界面
                mTabLayout.setCurrentTab(fragmentIndex);
    }

    private void initView() {
        mTabLayout = (CommonTabLayout) findViewById(R.id.tl);
        //给tab设置数据和关联的fragment
        mTabLayout.setTabData(mTabEntities, this, R.id.fl_change, mFragments);
        //设置红点
        mTabLayout.showDot(3);
    }

    private void initData() {
        mFragments.add(new HomeFragment());
        mFragments.add(new ServiceFragment());
        mFragments.add(new MarketFragment());
        mFragments.add(new NoticeFragment());
        mFragments.add(new MineFragment());
        //设置tab的标题、选中图标、未选中图标
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
